﻿using System;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using Common.Helper;
using Entities;
using Common.Constants;
using System.Globalization;
namespace DataAccess
{
    public class UserDataAccess : IUserDataAccess
    {
        /// <summary>
        /// Extracts the raw data.
        /// </summary>
        /// <param name="dataReader">The data reader.</param>
        /// <returns></returns>
        private static User ExtractRawData(IDataRecord dataReader)
        {
            return new User
                       {
                           UserId = (Guid)dataReader["UserId"],
                           Account = dataReader["Account"].ToString(),
                           Password = dataReader["Password"].ToString(),
                           Role = Int32.Parse(dataReader["Role"].ToString(),CultureInfo.InvariantCulture)
                       };   
        }

        /// <summary>
        /// Gets the user.
        /// </summary>
        /// <param name="account">The account.</param>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        public User GetUser(string account, string password)
        {
            if (String.IsNullOrEmpty(account) || String.IsNullOrEmpty(password)) return null;
            var colParameter = new Collection<SqlParameter>
                                                        {
                                                            new SqlParameter("Account",account),
                                                            new SqlParameter("Password",password)
                                                        };
            using (var reader = SqlHelper.ExecuteReader(ProcedureStores.GetUserWithPass, colParameter))
            {
                while (reader.Read())
                {
                    var result = ExtractRawData(reader);
                    return result;
                }
                return null;
            }
        }
        /// <summary>
        /// Gets the user by id.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <returns></returns>
        public User GetUserById(string userId)
        {
            var colParameter = new Collection<SqlParameter>
                                                        {
                                                            new SqlParameter("userId",userId)
                                                           
                                                        };
            using (var reader = SqlHelper.ExecuteReader(ProcedureStores.GetUserById, colParameter))
            {
                if (reader.Read())
                {
                    var result = ExtractRawData(reader);
                    return result;
                }
                return null;
            }
        }

        /// <summary>
        /// Gets the password.
        /// </summary>
        /// <param name="account">The account.</param>
        /// <returns></returns>
        public string GetPassword(string account)
        {
            if (String.IsNullOrEmpty(account)) return null;
            
            var colParameter = new Collection<SqlParameter>
                                   {
                                       new SqlParameter("Account",account)
                                   };
            using (var reader = SqlHelper.ExecuteReader(ProcedureStores.GetPassword,colParameter))
            {
                if(reader.Read())
                {
                    return reader["Password"].ToString();
                }
                return null;
            }
        }

        /// <summary>
        /// Gets the email.
        /// </summary>
        /// <param name="account">The account.</param>
        /// <returns></returns>
        public string GetEmail(string account)
        {
            if (String.IsNullOrEmpty(account)) return null;

            var colParameter = new Collection<SqlParameter>
                                   {
                                       new SqlParameter("Account",account)
                                   };
            using (var reader = SqlHelper.ExecuteReader(ProcedureStores.GetEmail, colParameter))
            {
                if (reader.Read())
                {
                    return reader["Email"].ToString();
                }
                return null;
            }
        }

        /// <summary>
        /// Gets the user.
        /// </summary>
        /// <param name="account">The account.</param>
        /// <returns></returns>
        public User GetUser(string account)
        {
            if (String.IsNullOrEmpty(account) ) return null;
            var colParameter = new Collection<SqlParameter>
                                                        {
                                                            new SqlParameter("Account",account)
                                                        };
            using (var reader = SqlHelper.ExecuteReader(ProcedureStores.GetUser, colParameter))
            {
                while (reader.Read())
                {
                    var result = ExtractRawData(reader);
                    return result;
                }
                return null;
            }
        }
    }
}
